package com.module_video.activity;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.load_state_view.MultiStateView;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.fastkit.views.spring_refresh.container.DefaultFooter;
import com.lib.fastkit.views.spring_refresh.container.DefaultHeader;
import com.lib.fastkit.views.spring_refresh.widget.SpringView;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.ui.activity.BaseAppActivity;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.utls.glide.GlideConfig;
import com.lib.view.navigationbar.NomalNavigationBar;
import com.module_video.R;
import com.module_video.R2;
import com.module_video.bean.ShortVideoListCommend;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * Created by Administrator on 2020/7/21 0021.
 */

@Route(path = ARouterPathUtils.Tablet_Video_ShortVideoListActivity)
public class ShortVideoListActivity extends BaseAppActivity {

    @BindView(R2.id.springView)
    SpringView springView;
    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.state_view)
    MultiStateView stateView;

    int commendPage = 0;

    List<ShortVideoListCommend.ObjBean.RowsBean> videoList = new ArrayList<>();
    VideoListAdapter videoListAdapter;

    @Override
    protected void onCreateView() {

        //初始化状态
        initStateView();

        initTitle();
        initView();

        //初始化推荐视频数据
        initRecommendTeacherVideos();

    }

    protected void initTitle() {
        NomalNavigationBar navigationBar = new
                NomalNavigationBar.Builder(this)
                .setTitle("短视频列表")
                .builder();

    }

    private void initView() {

        videoListAdapter = new VideoListAdapter(this, videoList);
        GridLayoutManager gm = new GridLayoutManager(this, 4);
        rvList.setLayoutManager(gm);
        rvList.setAdapter(videoListAdapter);

    }

    private void initStateView() {

        stateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        stateView.setMultiStateViewLisener(new MultiStateView.MultiStateViewLisener() {
            @Override
            public void onTryAgain() {
                initRecommendTeacherVideos();
            }
        });

        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));

        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {

                commendPage = 0;
                initRecommendTeacherVideos();

                springView.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {

                ++commendPage;
                initRecommendTeacherVideos();

                springView.onFinishFreshAndLoad();
            }
        });

    }

    private void initRecommendTeacherVideos() {

        HttpUtils.with(this)
                .addParam("requestType", "TEACHER_VIDEO_RECOMMENDATION")
                .addParam("token", SharedPreferenceManager.getInstance(this).getUserCache().getUserToken())
                .addParam("page", commendPage)
                .addParam("limit", 8)
                .execute(new HttpNormalCallBack<ShortVideoListCommend>() {
                    @Override
                    public void onSuccess(ShortVideoListCommend result) {
                        stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);

                        if (CodeUtil.CODE_200 == result.getCode()) {

                            if (result.getObj().getRows() != null && result.getObj().getRows().size() > 0) {
                                if (commendPage == 0) {
                                    if (videoList.size() > 0 && videoList.size() > 0) {
                                        videoList.clear();
                                    }
                                }
                                videoList.addAll(result.getObj().getRows());
                                videoListAdapter.notifyDataSetChanged();
                            }
                        }
                    }

                    @Override
                    public void onError(String e) {

                        stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                    }
                });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shortvideolist;
    }


    class VideoListAdapter extends BaseAdapter<ShortVideoListCommend.ObjBean.RowsBean> {

        public VideoListAdapter(Context context, List<ShortVideoListCommend.ObjBean.RowsBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_shortvideolist;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, int position, List<ShortVideoListCommend.ObjBean.RowsBean> mData) {

            final ShortVideoListCommend.ObjBean.RowsBean rowsBean = mData.get(position);

            ImageView ivBg = holder.getView(R.id.iv_bg);
//            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
//                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//            ivBg.setLayoutParams(params);
            if (StringUtil.isNotEmpty(rowsBean.getDynamic_hash_icon(), true)) {
                Glide.with(ShortVideoListActivity.this)
                        .load(rowsBean.getDynamic_hash_icon())
                        .apply(GlideConfig.getRoundOptions(15))
                        .into(ivBg);
            }

            if (StringUtil.isNotEmpty(rowsBean.getDynamic_content(), true)) {
                holder.setText(R.id.tv_title, rowsBean.getDynamic_content());
            }

            ImageView ivUser = holder.getView(R.id.iv_user);
            if (StringUtil.isNotEmpty(rowsBean.getPhoto_url(), true)) {
                Glide.with(ShortVideoListActivity.this)
                        .load(rowsBean.getPhoto_url())
                        .apply(GlideConfig.getCircleOptions())
                        .into(ivUser);
            }
            if (StringUtil.isNotEmpty(rowsBean.getUser_name(), true)) {
                holder.setText(R.id.tv_name, rowsBean.getUser_name());
            }

            //初始化点赞状态
            ImageView ivLike = holder.getView(R.id.iv_like);
            if (StringUtil.isNotEmpty(rowsBean.getLike_status() + "", true)) {

                if ("0".equals(rowsBean.getLike_status())) {
                    ivLike.setBackgroundResource(R.mipmap.icon_wgive);
                } else if ("1".equals(rowsBean.getLike_status())) {
                    ivLike.setBackgroundResource(R.mipmap.icon_give);
                }
            }
            if (StringUtil.isNotEmpty(rowsBean.getLike_total() + "", true)) {
                holder.setText(R.id.tv_num, rowsBean.getLike_total() + "");
            }

            LinearLayout linParent = holder.getView(R.id.lin_parent);
            linParent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int teacher_id = rowsBean.getUser_id();
                    int dynamic_id = rowsBean.getDynamic_id();
                    ARouter.getInstance().build(ARouterPathUtils.Tablet_Video_ShortVideoDetailActivity)
                            .withInt("teacher_id", teacher_id)
                            .withInt("dynamic_id", dynamic_id).navigation();
                }
            });

        }
    }
}
